
********************************************************************************************************************************
						
														***Marlene Mauk, Max Groemping***
			***Online Disinformation Predicts Inaccurate Beliefs about Election Fairness among Winners and Losers***
													***Replication File  ##1 - DATA PREPARATION***
						
********************************************************************************************************************************


**************************************************
          ***preparing the survey data***
**************************************************

//** World Values Survey Wave 7 **//
use WVS_Cross-National_Wave_7_Stata_v1_5.dta 
mvdecode _all, mv(-5/-1)

** variables for housekeeping
rename C_COW_NUM cowcode
rename B_COUNTRY isocode
rename B_COUNTRY_ALPHA iso
rename A_STUDY study
rename A_WAVE wave
gen year = floor(J_INTDATE/10000)
tab year A_YEAR /*these aren't exactly the same*/
replace year = A_YEAR if year==.
gen month = floor(J_INTDATE/100) - year*100
gen day = J_INTDATE - year*10000 - month*100
gen long date = mdy(month, day, year)
format date  %td

** DV: perceptions of electoral integrity
gen pef1 = 1-(Q224-1)/3
gen pef2 = (Q225-1)/3
gen pef6 = 1-((Q229-1)/3)
gen pef7 = (Q230-1)/3
gen pef8 = (Q231-1)/3
lab var pef1 "votes counted fairly"
lab var pef2 "opposition prevented from running (rec)"
lab var pef6 "election officials fair"
lab var pef7 "rich people buy elections (rec)"
lab var pef8 "violence at polls (rec)"

** satisfaction with democracy
gen satdem = (Q252-1)/9
lab var satdem "satisfaction with how political system functions"

** information-related variables
gen usesm = 4*(1-((Q207-1)/4))
lab var usesm "information source: social media"
lab def mediause 0 "never" 1 "less than monthly" 2 "monthly" 3 "weekly" 4 "daily"
lab val usesm mediause
gen polint = 1-((Q199-1)/3)
lab var polint "political interest"
recode Q57 (1=1) (2=0), gen(soctrust)
lab var soctrust "most people can be trusted"

** sociodemographics
rename Q275R edulvl_lmh
lab var edulvl_lmh "level of education lower middle high"
gen income = (Q288-1)/9
lab var income "income 10-cat"
rename Q262 age

keep Q223 pef* satdem polint usesm edulvl_lmh age income soctrust date year month day study wave cowcode isocode iso

save wvs7.dta, replace


//** European Values Study Wave 5 **//
do ZA7500_Merge_PATCH_1.do /*this fixes a bug in the EVS file*/
use ZA7500_v4-0-0.dta
mvdecode _all, mv(-5/-1)
mvdecode _all, mv(-10)

** variables for housekeeping
gen cowcode = .
replace cowcode=339 if country==8
replace cowcode=373 if country==31
replace cowcode=305 if country==40
replace cowcode=371 if country==51
replace cowcode=346 if country==70
replace cowcode=355 if country==100
replace cowcode=370 if country==112
replace cowcode=344 if country==191
replace cowcode=316 if country==203
replace cowcode=390 if country==208
replace cowcode=366 if country==233
replace cowcode=375 if country==246
replace cowcode=220 if country==250
replace cowcode=372 if country==268
replace cowcode=255 if country==276
replace cowcode=310 if country==348
replace cowcode=395 if country==352
replace cowcode=325 if country==380
replace cowcode=368 if country==440
replace cowcode=341 if country==499
replace cowcode=210 if country==528
replace cowcode=385 if country==578
replace cowcode=290 if country==616
replace cowcode=235 if country==620
replace cowcode=360 if country==642
replace cowcode=365 if country==643
replace cowcode=993 if country==688
replace cowcode=317 if country==703
replace cowcode=349 if country==705
replace cowcode=230 if country==724
replace cowcode=380 if country==752
replace cowcode=225 if country==756
replace cowcode=343 if country==807
replace cowcode=200 if country==826
rename country isocode
gen study=1
gen wave=5
rename year A_YEAR
gen year = floor(v277/10000)
tab year A_YEAR /*these aren't exactly the same*/
replace year = A_YEAR if year==.
gen month = floor(v277/100) - year*100
gen day = v277 - year*10000 - month*100
gen long date = mdy(month, day, year)
format date  %td

** DV: perceptions of electoral integrity
gen pef1 = 1-(v176-1)/3
gen pef2 = (v177-1)/3
gen pef6 = 1-((v181-1)/3)
gen pef7 = (v182-1)/3
gen pef8 = (v183-1)/3
lab var pef1 "votes counted fairly"
lab var pef2 "opposition prevented from running (rec)"
lab var pef6 "election officials fair"
lab var pef7 "rich people buy elections (rec)"
lab var pef8 "violence at polls (rec)"

** satisfaction with democracy
gen satdem = (v144-1)/9
lab var satdem "satisfaction with how political system functions"

** information-related variables
gen usesm = 4*(1-((v211-1)/4))
lab var usesm "information source: social media"
lab def mediause 0 "never" 1 "less often" 2 "once or twice a week" 3 "several times a week" 4 "every day"
lab val usesm mediause
gen polint = 1-((v97-1)/3)
lab var polint "political interest"
recode v31 (1=1) (2=0), gen(soctrust)
lab var soctrust "most people can be trusted"

** sociodemographics
mvdecode v243*, mv(66)
rename v243_r edulvl_lmh
lab var edulvl_lmh "level of education lower middle high"
gen income = (v261-1)/9
lab var income "income 10-cat"
tab age

keep v174_cs pef* satdem polint usesm edulvl_lmh age income soctrust date year month day study wave cowcode isocode

save evs5.dta, replace


//** World Values Study Wave 6 **//
use WV6_Data_Stata_v20180912.dta
mvdecode _all, mv(-5/-1)

** variables for housekeeping
rename COW cowcode
rename V2 isocode
rename S001 study
rename V1 wave
gen year = floor(V261/10000)
gen month = floor(V261/100) - year*100
gen day = V261 - year*10000 - month*100
gen long date = mdy(month, day, year)
format date  %td
sum date, format
tab year V262 /*these aren't exactly the same!*/
replace year = V262 if year==.

** DV: perceptions of electoral fairness
gen pef1 = 1-((V228A-1)/3)
gen pef2 = (V228B-1)/3
gen pef6 = 1-((V228F-1)/3)
gen pef7 = (V228G-1)/3
gen pef8 = (V228H-1)/3
lab var pef1 "votes counted fairly"
lab var pef2 "opposition prevented from running (rec)"
lab var pef6 "election officials fair"
lab var pef7 "rich people buy elections (rec)"
lab var pef8 "violence at polls (rec)"

** satisfaction with democracy
* WVS 6 doesn't ask about satisfaction with democracy

** information-related variables
* WVS 6 doesn't ask about social media use
gen polint = 1-((V84-1)/3)
lab var polint "political interest"
recode V24 (1=1) (2=0) (else=.), gen(soctrust)
lab var soctrust "most people can be trusted"

** sociodemographics
recode V248 (1/4=1) (5/7=2) (8/9=3) (else=.), gen(edulvl_lmh) /*this mirrors the coding done in WVS 7 and EVS 5 (based on ISCED)*/
lab var edulvl_lmh "level of education"
rename V242 age
gen income = (V239-1)/9
lab var income "income 10-cat"

drop if year<2012
keep V228 pef* polint edulvl_lmh age income soctrust date year month day study wave cowcode isocode

save wvs6.dta, replace


//** merging the survey data and preparing it for merge with macro data **//
use wvs7.dta
append using evs5.dta
append using wvs6.dta

recode usesm (0=0) (1=1) (2=2) (3=3) (4=5) if study==2 /*harmonize response category coding across surveys*/
recode usesm (0=0) (1=1) (2=3) (3=4) (4=5) if study==1
lab drop mediause
lab def mediause 0 "never" 1 "less than monthly" 2 "monthly" 3 "weekly" 4 "several times a week" 5 "daily"
lab val usesm mediause
recode usesm (0/2=0) (3/5=1), gen(usesm01)
recode usesm (0/4=0) (5=1), gen(powerusesm)
gen edu_1=.
gen edu_2=.
gen edu_3=.
replace edu_1=1 if edulvl_lmh==1
replace edu_1=0 if edulvl_lmh==2 | edulvl_lmh==3
replace edu_2=1 if edulvl_lmh==2
replace edu_2=0 if edulvl_lmh==1 | edulvl_lmh==3
replace edu_3=1 if edulvl_lmh==3
replace edu_3=0 if edulvl_lmh==1 | edulvl_lmh==2
replace age = 82 if age>82 /*as EVS only registers age up to 81, need to recode WVS data to reflect that (that's how they did it in the joint EVS-WVS file as well)*/

lab drop C_COW_NU
lab define cowcode 2 "USA" 6 "Puerto Rico" 20 "Canada" 31 "Bahamas" 40 "Cuba" 41 "Haiti" 42 "Dominican Republic" 51 "Jamaica" 52 "Trinidad and Tobago" 53 "Barbados" 54 "Dominica" 55 "Grenada" 56 "St. Lucia" 57 "St. Vincent and the Grenadines" 58 "Antigua & Barbuda" 60 "St. Kitts and Nevis" 70 "Mexico" 80 "Belize" 90 "Guatemala" 91 "Honduras" 92 "El Salvador" 93 "Nicaragua" 94 "Costa Rica" 95 "Panama" 100 "Colombia" 101 "Venezuela" 110 "Guyana" 115 "Suriname" 130 "Ecuador" 135 "Peru" 140 "Brazil" 145 "Bolivia" 150 "Paraguay" 155 "Chile" 160 "Argentina" 165 "Uruguay" 200 "United Kingdom" 205 "Ireland" 210 "Netherlands" 211 "Belgium" 212 "Luxembourg" 220 "France" 221 "Monaco" 223 "Liechtenstein" 225 "Switzerland" 230 "Spain" 232 "Andorra" 235 "Portugal" 255 "Germany" 290 "Poland" 305 "Austria" 310 "Hungary" 316 "Czechia" 317 "Slovakia" 325 "Italy" 331 "San Marino" 338 "Malta" 339 "Albania" 341 "Montenegro" 343 "North Macedonia" 344 "Croatia" 345 "Serbia" 346 "Bosnia and Herzegovina" 347 "Kosovo" 349 "Slovenia" 350 "Greece" 352 "Cyprus" 355 "Bulgaria" 359 "Moldova" 360 "Romania" 365 "Russia" 366 "Estonia" 367 "Latvia" 368 "Lithuania" 369 "Ukraine" 370 "Belarus" 371 "Armenia" 372 "Georgia" 373 "Azerbaijan" 375 "Finland" 380 "Sweden" 385 "Norway" 390 "Denmark" 395 "Iceland" 402 "Cabo Verde" 403 "Sao Tome and Principe" 404 "Guinea-Bissau" 411 "Equatorial Guinea" 420 "Gambia" 432 "Mali" 433 "Senegal" 434 "Benin" 435 "Mauritania" 436 "Niger" 437 "Côte d'Ivoire" 438 "Guinea" 439 "Burkina Faso" 446 "Macau" 450 "Liberia" 451 "Sierra Leone" 452 "Ghana" 461 "Togo" 471 "Cameroon" 475 "Nigeria" 481 "Gabon" 482 "Central African Republic" 483 "Chad" 484 "Congo" 490 "Democratic Republic of the Congo" 500 "Uganda" 501 "Kenya" 510 "Tanzania" 511 "Zanzibar" 516 "Burundi" 517 "Rwanda" 520 "Somalia" 522 "Djibouti" 530 "Ethiopia" 531 "Eritrea" 540 "Angola" 541 "Mozambique" 551 "Zambia" 552 "Zimbabwe" 553 "Malawi" 560 "South Africa" 565 "Namibia" 570 "Lesotho" 571 "Botswana" 572 "eSwatini" 580 "Madagascar" 581 "Comoros" 590 "Mauritius" 591 "Seychelles" 600 "Morocco" 615 "Algeria" 616 "Tunisia" 620 "Libya" 625 "Sudan" 626 "South Sudan" 630 "Iran" 640 "Turkey" 645 "Iraq" 651 "Egypt" 652 "Syria" 660 "Lebanon" 663 "Jordan" 666 "Israel" 670 "Saudi Arabia" 679 "Yemen" 690 "Kuwait" 692 "Bahrain" 694 "Qatar" 696 "United Arab Emirates" 698 "Oman" 700 "Afghanistan" 701 "Turkmenistan" 702 "Tajikistan" 703 "Kyrgyzstan" 704 "Uzbekistan" 705 "Kazakhstan" 710 "China" 712 "Mongolia" 713 "Taiwan" 714 "Hong Kong" 731 "North Korea" 732 "South Korea" 740 "Japan" 750 "India" 760 "Bhutan" 770 "Pakistan" 771 "Bangladesh" 775 "Myanmar" 780 "Sri Lanka" 781 "Maldives" 790 "Nepal" 800 "Thailand" 811 "Cambodia" 812 "Laos" 816 "Vietnam" 820 "Malaysia" 830 "Singapore" 835 "Brunei" 840 "Philippines" 850 "Indonesia" 860 "Timor-Leste" 900 "Australia" 910 "Papua New Guinea" 920 "New Zealand" 935 "Vanuatu" 940 "Solomon Islands" 946 "Kiribati" 947 "Tuvalu" 950 "Fiji" 955 "Tonga" 970 "Nauru" 983 "Marshall Islands" 986 "Palau" 987 "Federated State of Micronesia" 990 "Samoa" 991 "Palestine West Bank" 993 "Serbia"
lab val cowcode cowcode

** check survey fieldwork dates and generate election-year variable based on date of mose recent election
sort cowcode
bysort cowcode: sum date, format
sum date, format, if cowcode==993 & study==1
sum date, format, if cowcode==993 & study==2
sum date, format, if cowcode==255 & study==1
sum date, format, if cowcode==255 & study==2
sum date, format, if cowcode==360 & study==1
sum date, format, if cowcode==360 & study==2
sum date, format, if cowcode==365 & study==1
sum date, format, if cowcode==365 & study==2

sum date, format, if cowcode==341
sum year if cowcode==130

gen split=.
replace split=1 if cowcode==310 & year==2018 & month>4 /*there were elections in Hungary on 08/04/2018*/
replace split=1 if cowcode==310 & year==2018 & month==4 & day>8 /*leave out election day itself as it's unclear whether interview was conducted before or after respondent went out to vote*/
replace split=2 if cowcode==310 & year==2018 & month==4 & day<8
replace split=2 if cowcode==310 & year==2018 & month<4
replace split=1 if cowcode==316 & year==2017 & month>10 /*there were elections in Czechia on 21/10/2017*/
replace split=1 if cowcode==316 & year==2017 & month==10 & day>21
replace split=2 if cowcode==316 & year==2017 & month==10 & day<21
replace split=2 if cowcode==316 & year==2017 & month<10
replace split=1 if cowcode==349 & year==2017 & month>11 /*there were elections in Slovenia on 12/11/2017*/
replace split=1 if cowcode==349 & year==2017 & month==11 & day>12
replace split=2 if cowcode==349 & year==2017 & month==11 & day<12
replace split=2 if cowcode==349 & year==2017 & month<11
replace split=1 if cowcode==375 & year==2018 & month>1 /*there were elections in Finland on 28/01/2018*/
replace split=1 if cowcode==375 & year==2018 & month==1 & day>28
replace split=2 if cowcode==375 & year==2018 & month==1 & day<28
replace split=2 if cowcode==375 & year==2017
replace split=1 if cowcode==395 & year==2018 /*there were elections in Iceland on 28/10/2017*/
replace split=1 if cowcode==395 & year==2017 & month>10
replace split=1 if cowcode==395 & year==2017 & month==10 & day>28
replace split=2 if cowcode==395 & year==2017 & month==10 & day<28
replace split=2 if cowcode==395 & year==2017 & month<10
replace split=1 if cowcode==820 & year==2018 & month>5 /*there were elections in Malaysia on 09/05/2018*/
replace split=1 if cowcode==820 & year==2018 & month==5 & day>9
replace split=2 if cowcode==820 & year==2018 & month==5 & day<9
replace split=2 if cowcode==820 & year==2018 & month<5
replace split=1 if cowcode==360 & year==2012 & month>12 /*there were elections in Romania on 09/12/2012*/
replace split=1 if cowcode==360 & year==2012 & month==12 & day>9
replace split=2 if cowcode==360 & year==2012 & month==12 & day<9
replace split=2 if cowcode==360 & year==2012 & month<12
replace split=1 if cowcode==255 & year==2013 & month>9 /*there were elections in Germany on 22/09/2013*/
replace split=1 if cowcode==255 & year==2013 & month==9 & day>22
replace split=2 if cowcode==255 & year==2013 & month==9 & day<22
replace split=2 if cowcode==255 & year==2013 & month<9

gen eleyear=.
replace eleyear=2019 if cowcode==235 | cowcode==369
replace eleyear=2018 if cowcode==100 | cowcode==341 | cowcode==325 | cowcode==346 | cowcode==352 | cowcode==373 | cowcode==552 | cowcode==645 | cowcode==651 | cowcode==660 | cowcode==770
replace eleyear=2017 if cowcode==130 | cowcode==155 | cowcode==200 | cowcode==210 | cowcode==220 | cowcode==305 | cowcode==339 | cowcode==355 | cowcode==371 | cowcode==385 | cowcode==630 | cowcode==703 | cowcode==732 | cowcode==740 | cowcode==920 | cowcode==255 | cowcode==993
replace eleyear=2016 if cowcode==2 | cowcode==93 | cowcode==135 | cowcode==230 | cowcode==317 | cowcode==343 | cowcode==344 | cowcode==368 | cowcode==370 | cowcode==372 | cowcode==663 | cowcode==705 | cowcode==713 | cowcode==816 | cowcode==840 | cowcode==900 | cowcode==360 | cowcode==365
replace eleyear=2015 if cowcode==70 | cowcode==90 | cowcode==160 | cowcode==225 | cowcode==290 | cowcode==350 | cowcode==366 | cowcode==390 | cowcode==475 | cowcode==530 | cowcode==640 | cowcode==702 | cowcode==775
replace eleyear=2014 if cowcode==140 | cowcode==145 | cowcode==380 | cowcode==616 | cowcode==771 | cowcode==800 | cowcode==850
replace eleyear=. if wave==6
replace eleyear=2015 if cowcode==41
replace eleyear=2013 if (cowcode==130 & wave==6) | (cowcode==372 & wave==6) | (cowcode==663 & wave==6) | (cowcode==690 & wave==6)
replace eleyear=2012 if (cowcode==210 & wave==6)
replace eleyear=2018 if cowcode==310 & split==1
replace eleyear=2014 if cowcode==310 & split==2
replace eleyear=2017 if cowcode==316 & split==1
replace eleyear=2013 if cowcode==316 & split==2
replace eleyear=2017 if cowcode==349 & split==1
replace eleyear=2014 if cowcode==349 & split==2
replace eleyear=2018 if cowcode==375 & split==1
replace eleyear=2015 if cowcode==375 & split==2
replace eleyear=2017 if cowcode==395 & split==1
replace eleyear=2016 if cowcode==395 & split==2
replace eleyear=2018 if cowcode==820 & split==1
replace eleyear=2013 if cowcode==820 & split==2
replace eleyear=2012 if cowcode==360 & split==1
replace eleyear=2013 if cowcode==255 & split==1 /*Germany 2013 could not be splitted due to missing interview dates*/

tab cowcode year if eleyear==. /*that's countries without elections / countries not included in PEI data / countries where last election was before 2012 / respondents interviewed on election day */
drop if eleyear==. & wave==6 & cowcode!=360 /*drop WVS 6 countries for which we don't have PEI data*/
tab cowcode if eleyear==.

** generate variable to indicate whether or not observation has to be dropped from analysis (because election took place before 2012 etc) and drop those observations
gen drop = 0
replace drop = 1 if (wave==6 & cowcode==452) | (wave==6 & cowcode==70) | (wave==6 & cowcode==360 & split==2) | (wave==6 & cowcode==160) | (wave==6 & cowcode==130) | (wave==6 & cowcode==255 & split==2) | (wave==6 & cowcode==615) | (wave==6 & cowcode==140) | (wave==6 & cowcode==690)
replace drop = 1 if (wave==7 & cowcode==6) | (wave==7 & cowcode==90) | (wave==7 & cowcode==232) | (wave==7 & cowcode==446) | (wave==7 & cowcode==663) | (wave==7 & cowcode==710) | (wave==7 & cowcode==714) | (wave==5 & cowcode==235) | (wave==7 & cowcode==369)
drop if drop==1

** add ISO alphabetical code for countries it wasn't yet included
numlabel _all, add force
tab isocode if iso==""
replace iso="ALB" if isocode==8
replace iso="AZE" if isocode==31
replace iso="AUT" if isocode==40
replace iso="ARM" if isocode==51
replace iso="BIH" if isocode==70
replace iso="BGR" if isocode==100
replace iso="BLR" if isocode==112
replace iso="HRV" if isocode==191
replace iso="CZE" if isocode==203
replace iso="DNK" if isocode==208
replace iso="ECU" if isocode==218
replace iso="EST" if isocode==233
replace iso="FIN" if isocode==246
replace iso="FRA" if isocode==250
replace iso="GEO" if isocode==268
replace iso="DEU" if isocode==276
replace iso="HTI" if isocode==332
replace iso="HUN" if isocode==348
replace iso="ISL" if isocode==352
replace iso="ITA" if isocode==380
replace iso="JOR" if isocode==400
replace iso="KWT" if isocode==414
replace iso="LTU" if isocode==440
replace iso="MNE" if isocode==499
replace iso="NLD" if isocode==528
replace iso="NOR" if isocode==578
replace iso="POL" if isocode==616
replace iso="PRT" if isocode==620
replace iso="ROU" if isocode==642
replace iso="RUS" if isocode==643
replace iso="SRB" if isocode==688
replace iso="SVK" if isocode==703
replace iso="SVN" if isocode==705
replace iso="ESP" if isocode==724
replace iso="SWE" if isocode==752
replace iso="CHE" if isocode==756
replace iso="UKR" if isocode==807
replace iso="GBR" if isocode==826

** generate election variable for merge with PEI data
gen election = . 
tostring election, replace
replace election="USA_08112016_P1" if cowcode==2
replace election="MEX_07062015_L1" if cowcode==70
replace election="GTM_25102015_P2" if cowcode==90
replace election="NIC_06112016_P1" if cowcode==93
replace election="COL_17062018_P2" if cowcode==100
replace election="ECU_02042017_P2" if cowcode==130
replace election="PER_05062016_P2" if cowcode==135
replace election="BRA_26102014_P2" if cowcode==140
replace election="BOL_12102014_P1" if cowcode==145
replace election="CHL_17122017_P2" if cowcode==155
replace election="ARG_22112015_L1" if cowcode==160
replace election="GBR_08062017_L1" if cowcode==200
replace election="NLD_15032017_L1" if cowcode==210
replace election="FRA_07052017_P2" if cowcode==220
replace election="CHE_18102015_L1" if cowcode==225
replace election="ESP_26062016_L1" if cowcode==230
replace election="DEU_24092017_L1" if cowcode==255
replace election="POL_24052015_P2" if cowcode==290
replace election="AUT_15102017_L1" if cowcode==305
replace election="SVK_05032016_L1" if cowcode==317
replace election="ITA_04032018_L1" if cowcode==325
replace election="ALB_25062017_L1" if cowcode==339
replace election="MNE_15042018_P1" if cowcode==341
replace election="MKD_11122016_L1" if cowcode==343
replace election="HRV_11092016_L1" if cowcode==344
replace election="BIH_07102018_P1" if cowcode==346
replace election="GRC_20092015_L1" if cowcode==350
replace election="CYP_04022018_P2" if cowcode==352
replace election="BGR_26032017_L1" if cowcode==355
replace election="ROU_11122016_L1" if cowcode==360
replace election="RUS_18092016_L1" if cowcode==365
replace election="EST_01032015_L1" if cowcode==366
replace election="LTU_09102016_L1" if cowcode==368
replace election="BLR_11092016_L1" if cowcode==370
replace election="ARM_02042017_L1" if cowcode==371
replace election="GEO_08102016_L1" if cowcode==372
replace election="AZE_11042018_P1" if cowcode==373
replace election="SWE_14092014_L1" if cowcode==380
replace election="NOR_09112017_L1" if cowcode==385
replace election="DNK_18062015_L1" if cowcode==390
replace election="NGA_28032015_L1" if cowcode==475
replace election="ETH_24052015_L1" if cowcode==530
replace election="ZWE_30072018_P1" if cowcode==552
replace election="TUN_21122014_P2" if cowcode==616
replace election="IRN_19052017_P1" if cowcode==630
replace election="TUR_01112015_L1" if cowcode==640
replace election="IRQ_12052018_L1" if cowcode==645
replace election="EGY_28032018_P1" if cowcode==651
replace election="LBN_06052018_L1" if cowcode==660
replace election="JOR_20092016_L1" if cowcode==663
replace election="TJK_01032015_L1" if cowcode==702
replace election="KGZ_15102017_P1" if cowcode==703
replace election="KAZ_20032016_L1" if cowcode==705
replace election="TWN_16012016_P1" if cowcode==713
replace election="KOR_09052017_P1" if cowcode==732
replace election="JPN_22102017_L1" if cowcode==740
replace election="PAK_25072018_L1" if cowcode==770
replace election="BGD_05012014_L1" if cowcode==771
replace election="MMR_08112015_L1" if cowcode==775
replace election="THA_02022014_L1" if cowcode==800
replace election="VNM_22052016_L1" if cowcode==816
replace election="PHL_09052016_P1" if cowcode==840
replace election="IDN_09072014_P1" if cowcode==850
replace election="AUS_02072016_L1" if cowcode==900
replace election="NZL_23092017_L1" if cowcode==920
replace election="SRB_02042017_P1" if cowcode==993

replace election="NLD_12092012_L1" if cowcode==210 & eleyear==2012
replace election="GEO_27102013_P1" if cowcode==372 & eleyear==2013
replace election="JOR_23012013_L1" if cowcode==663 & eleyear==2013
replace election="KWT_27072013_L1" if cowcode==690 & eleyear==2013
replace election="HTI_25102015_L2" if cowcode==41 & eleyear==2015

replace election="HUN_08042018_L1" if cowcode==310 & eleyear==2018
replace election="HUN_06042014_L1" if cowcode==310 & eleyear==2014
replace election="CZE_21102017_L1" if cowcode==316 & eleyear==2017
replace election="CZE_25102013_L1" if cowcode==316 & eleyear==2013
replace election="SVN_12112017_P2" if cowcode==349 & eleyear==2017
replace election="SVN_13072014_L1" if cowcode==349 & eleyear==2014
replace election="FIN_28012018_P1" if cowcode==375 & eleyear==2018
replace election="FIN_19042015_L1" if cowcode==375 & eleyear==2015
replace election="ISL_28102017_L1" if cowcode==395 & eleyear==2017
replace election="ISL_25062016_P1" if cowcode==395 & eleyear==2016
replace election="MYS_09052018_L1" if cowcode==820 & eleyear==2018
replace election="MYS_05052013_L1" if cowcode==820 & eleyear==2013
replace election="ROU_09122012_L1" if cowcode==360 & eleyear==2012
replace election="DEU_22092013_L1" if cowcode==255 & eleyear==2013

** create country-electionyear variable
gen elecow = 1000*eleyear+cowcode

save survey.dta, replace


** coding winner/loser variable
//*extract government composition from WhoGov*//
clear
import delimited using WhoGov_within_V1.1.csv, varnames(1) bindquote(strict)
drop v1
drop if year <= 2010
drop if (minister==0 & leader==0)
list party party_english minister leader if year==2017 & country_isocode=="USA"
list party party_english minister leader if year==2017 & country_isocode=="MEX"
list party party_english minister leader if year==2019 & country_isocode=="GTM"
list party party_english minister leader if year==2019 & country_isocode=="NIC"
list party party_english minister leader if year==2018 & country_isocode=="COL"
list party party_english minister leader if year==2017 & country_isocode=="ECU"
list party party_english minister leader if year==2016 & country_isocode=="PER"
list party party_english minister leader if year==2016 & country_isocode=="BRA"
list party party_english minister leader if year==2016 & country_isocode=="BOL"
list party party_english minister leader if year==2018 & country_isocode=="CHL"
list party party_english minister leader if year==2017 & country_isocode=="ARG"
list party party_english minister leader if year==2017 & country_isocode=="GBR"
list party party_english minister leader if year==2018 & country_isocode=="NLD"
list party party_english minister leader if year==2018 & country_isocode=="FRA"
list party party_english minister leader if year==2017 & country_isocode=="CHE"
list party party_english minister leader if year==2017 & country_isocode=="ESP"
list party party_english minister leader if year==2018 & country_isocode=="DEU"
list party party_english minister leader if year==2016 & country_isocode=="POL"
list party party_english minister leader if year==2018 & country_isocode=="AUT"
list party party_english minister leader if year==2016 & country_isocode=="HUN"
list party party_english minister leader if year==2018 & country_isocode=="MNE"
list party party_english minister leader if year==2016 & country_isocode=="CZE"
list party party_english minister leader if year==2016 & country_isocode=="SVK"
list party party_english minister leader if year==2018 & country_isocode=="ITA"
list party party_english minister leader if year==2017 & country_isocode=="ALB"
list party party_english minister leader if year==2017 & country_isocode=="MKD"
list party party_english minister leader if year==2017 & country_isocode=="HRV"
list party party_english minister leader if year==2017 & country_isocode=="SRB"
list party party_english minister leader if year==2019 & country_isocode=="BIH"
list party party_english minister leader if year==2016 & country_isocode=="SVN"
list party party_english minister leader if year==2016 & country_isocode=="GRC"
list party party_english minister leader if year==2018 & country_isocode=="CYP"
list party party_english minister leader if year==2017 & country_isocode=="BGR"
list party party_english minister leader if year==2017 & country_isocode=="ROU"
list party party_english minister leader if year==2017 & country_isocode=="RUS"
list party party_english minister leader if year==2017 & country_isocode=="EST"
list party party_english minister leader if year==2017 & country_isocode=="LTU"
list party party_english minister leader if year==2017 & country_isocode=="BLR"
list party party_english minister leader if year==2017 & country_isocode=="ARM"
list party party_english minister leader if year==2017 & country_isocode=="GEO"
list party party_english minister leader if year==2018 & country_isocode=="AZE"
list party party_english minister leader if year==2018 & country_isocode=="FIN"
list party party_english minister leader if year==2017 & country_isocode=="FIN"
list party party_english minister leader if year==2017 & country_isocode=="SWE"
list party party_english minister leader if year==2018 & country_isocode=="NOR"
list party party_english minister leader if year==2017 & country_isocode=="DNK"
list party party_english minister leader if year==2018 & country_isocode=="ISL"
list party party_english minister leader if year==2017 & country_isocode=="ISL"
list party party_english minister leader if year==2016 & country_isocode=="NGA"
list party party_english minister leader if year==2016 & country_isocode=="ETH"
list party party_english minister leader if year==2019 & country_isocode=="ZWE"
list party party_english minister leader if year==2016 & country_isocode=="TUN"
list party party_english minister leader if year==2017 & country_isocode=="IRN"
list party party_english minister leader if year==2016 & country_isocode=="TUR"
list party party_english minister leader if year==2018 & country_isocode=="IRQ"
list party party_english minister leader if year==2018 & country_isocode=="EGY"
list party party_english minister leader if year==2018 & country_isocode=="LBN"
list party party_english minister leader if year==2017 & country_isocode=="JOR"
list party party_english minister leader if year==2016 & country_isocode=="TJK"
list party party_english minister leader if year==2018 & country_isocode=="KGZ"
list party party_english minister leader if year==2016 & country_isocode=="KAZ"
list party party_english minister leader if year==2016 & country_isocode=="TWN"
list party party_english minister leader if year==2017 & country_isocode=="KOR"
list party party_english minister leader if year==2018 & country_isocode=="JPN"
list party party_english minister leader if year==2018 & country_isocode=="PAK"
list party party_english minister leader if year==2016 & country_isocode=="BGD"
list party party_english minister leader if year==2016 & country_isocode=="MMR"
list party party_english minister leader if year==2016 & country_isocode=="THA"
list party party_english minister leader if year==2016 & country_isocode=="VNM"
list party party_english minister leader if year==2016 & country_isocode=="MYS"
list party party_english minister leader if year==2018 & country_isocode=="PHL"
list party party_english minister leader if year==2016 & country_isocode=="IDN"
list party party_english minister leader if year==2017 & country_isocode=="AUS"
list party party_english minister leader if year==2018 & country_isocode=="NZL"
list party party_english minister leader if year==2018 & country_isocode=="SRB"
list party party_english minister leader if year==2013 & country_isocode=="NLD"
list party party_english minister leader if year==2013 & country_isocode=="ROU"
list party party_english minister leader if year==2013 & country_isocode=="ECU"
list party party_english minister leader if year==2014 & country_isocode=="DEU"
list party party_english minister leader if year==2014 & country_isocode=="GEO"
list party party_english minister leader if year==2013 & country_isocode=="JOR"
list party party_english minister leader if year==2016 & country_isocode=="HTI"
clear

//*based on WhoGov government composition, recode vote choice variables from surveys*//
use survey.dta
gen winner=. /*1=winner, 0=loser, 2=other, 3=would not vote/cast blank ballot/none of these, 4=not eligible, 5=independent*/

/*recoding WVS 7 countries*/
replace winner=2 if Q223==5 & study==2
replace winner=3 if (Q223==4 & study==2) | (Q223==7 & study==2) | (Q223==3 & study==2) | (Q223==2 & study==2)
replace winner=4 if Q223==1 & study==2
replace winner=5 if Q223==8

/*USA 2017*/
replace winner=1 if Q223==840001
replace winner=0 if Q223==840002 | Q223==840004 | Q223==840006
/*Mexico 2018*/
replace winner=1 if Q223==484001 | Q223==484005
replace winner=0 if Q223==484002 | Q223==484003 | Q223==484004 | Q223==484017 | Q223==484018 | Q223==484022 | Q223==484023
/*Nicaragua 2019-2020*/
replace winner=1 if Q223==558001
replace winner=0 if Q223==558002 | Q223==558003 | Q223==558004 | Q223==558005
/*Colombia 2018*/
replace winner=1 if Q223==170021 | Q223==170001 | Q223==170004
replace winner=0 if Q223==170002 | Q223==170003 | Q223==170005 | Q223==170006 | Q223==170008 | Q223==170020
/*Ecuador 2018*/
replace winner=1 if Q223==218001
replace winner=0 if Q223==218004 | Q223==218006 | Q223==218008 | Q223==218014 | Q223==218015 | Q223==218018 | Q223==218019 | Q223==218020
/*Peru 2018*/
replace winner=1 if Q223==604046
replace winner=0 if Q223==604002 | Q223==604006 | Q223==604007 | Q223==604017 | Q223==604021 | Q223==604023 | Q223==604026 | Q223==604027 | Q223==604035 | Q223==604036 | Q223==604039 | Q223==604040 | Q223==604042 | Q223==604044 | Q223==604047 | Q223==604048
/*Brazil 2018*/
replace winner=1 if Q223==76001 | Q223==76003 | Q223==76005
replace winner=0 if Q223==76002 | Q223==76004 | Q223==76009 | Q223==76015 | Q223==76026 | Q223==76028 | Q223==76029
/*Bolivia 2017*/
replace winner=1 if Q223==68001
replace winner=0 if Q223==68002 | Q223==68003 | Q223==68004 | Q223==68005
/*Chile 2018*/
replace winner=1 if Q223==152002 | Q223==152029 | Q223==152001 | Q223==152007
replace winner=0 if Q223==152004 | Q223==152005 | Q223==152006 | Q223==152008 | Q223==152009 | Q223==152016 | Q223==152023 | Q223==152024 | Q223==152025 | Q223==152026 | Q223==152027 | Q223==152030 | Q223==152031
/*Argentina 2017*/
replace winner=1 if Q223==32105 | Q223==32012 | Q223==32009 /*Together for CHange = Cambiemos = support coalition for president Macri*/
replace winner=0 if Q223==32001 | Q223==32012 | Q223==32101
/*Germany 2017-2018*/
replace winner=1 if Q223==276001 | Q223==276002
replace winner=0 if Q223==276003 | Q223==276004 | Q223==276033 | Q223==276034
/*Greece 2017*/
replace winner=1 if Q223==300110 | Q223==300101
replace winner=0 if Q223==300002 | Q223==300003 | Q223==300102 | Q223==300103 | Q223==300104 | Q223==300106 | Q223==300108 | Q223==300109 | Q223==300111
/*Cyprus 2019*/
replace winner=1 if Q223==196002
replace winner=0 if Q223==196001 | Q223==196003 | Q223==196004 | Q223==196006 | Q223==196009 | Q223==196010 | Q223==196011 | Q223==196015 | Q223==196016 | Q223==196021 | Q223==196025 | Q223==196026 | Q223==196027 | Q223==196028 | Q223==196029 | Q223==196031
/*Romania 2017-2018*/
replace winner=1 if Q223==642008 | Q223==642066
replace winner=0 if Q223==642006 | Q223==642007 | Q223==642060 | Q223==642065 | Q223==642067 | Q223==642068
/*Russia 2017*/
replace winner=1 if Q223==643032 | Q223==643043 | Q223==643033 /*Rodina and Patriots of Russia are party of All Russia Front*/
replace winner=0 if Q223==643002 | Q223==643003 | Q223==643022 | Q223==643030 | Q223==643034 | Q223==643037 | Q223==643040 | Q223==643041 | Q223==643042 | Q223==643044 | Q223==643045
/*Nigeria 2017-2018*/
replace winner=1 if Q223==566021
replace winner=0 if Q223==566005 | Q223==566012 | Q223==566022 | Q223==566023 | Q223==566035 | Q223==566036
/*Ethiopia 2020*/
replace winner=1 if Q223==231028 /*Prosperity Party is successor party of EPRDF*/
replace winner=0 if Q223==231005 | Q223==231029 | Q223==231030 | Q223==231032 | Q223==231033 | Q223==231034
/*Zimbabwe 2020*/
replace winner=1 if Q223==716027
replace winner=0 if Q223==716002 | Q223==716010 | Q223==716015 | Q223==716016 | Q223==716017 | Q223==716018 | Q223==716019 | Q223==716020 | Q223==716021 | Q223==716022 | Q223==716023 | Q223==716024 | Q223==716025 | Q223==716026 | Q223==716028 | Q223==716029 | Q223==716031 | Q223==716032 | Q223==716033 | Q223==716034
/*Tunisia 2019*/
replace winner=1 if Q223==788022 | Q223==788011 | Q223==788023
replace winner=0 if Q223==788007 | Q223==788012 | Q223==788013 | Q223==788014 | Q223==788015 | Q223==788016 | Q223==788017 | Q223==788019 | Q223==788020 | Q223==788024
/*Iran 2020*/
replace winner=1 if Q223==364012
replace winner=0 if Q223==364011
/*Turkey 2018*/
replace winner=1 if Q223==792008
replace winner=0 if Q223==792002 | Q223==792005 | Q223==792006 | Q223==792050 | Q223==792051
/*Iraq 2018*/
replace winner=1 if Q223==368046 | Q223==368003 | Q223==368005
replace winner=0 if Q223==368004 | Q223==368007 | Q223==368008 | Q223==368021 | Q223==368041 | Q223==368042 | Q223==368043 | Q223==368044 | Q223==368045 | Q223==368048 | Q223==368050 | Q223==368051 | Q223==368054 | Q223==368055 | Q223==368056 | Q223==368059 | Q223==368061 | Q223==368062 | Q223==368066 | Q223==368067 | Q223==368068 | Q223==368069 | Q223==368073 | Q223==368077
/*Egypt 2018*/
/*cabinet is Independents and Egypt Party, which wasn't questioned in WVS*/
replace winner=0 if Q223==818002 | Q223==818103 | Q223==818127 | Q223==818141 | Q223==818159 | Q223==818160 | Q223==818161 | Q223==818164 | Q223==818165 | Q223==818166 | Q223==818167
/*Lebanon 2018*/
replace winner=1 if Q223==422003 | Q223==422002 | Q223==422001 | Q223==422018 | Q223==422005 | Q223==422009 | Q223==422004 | Q223==422015
replace winner=0 if Q223==422006 | Q223==422007 | Q223==422010 | Q223==422011 | Q223==422016 | Q223==422017 | Q223==422019 | Q223==422020 | Q223==422021 | Q223==422022 | Q223==422024 | Q223==422026 | Q223==422028
/*Tajikistan 2020*/
replace winner=1 if Q223==762001
replace winner=0 if Q223==762002 | Q223==762003 | Q223==762004 | Q223==762005 | Q223==762006
/*Kyrgyzstan 2019-2020*/
replace winner=1 if Q223==417038
replace winner=0 if Q223==417002 | Q223==417006 | Q223==417009 | Q223==417010 | Q223==417012 | Q223==417013 | Q223==41701 | Q223==417018 | Q223==417039 | Q223==417105 | Q223==417106 | Q223==417111
/*Kazakhstan 2018*/
replace winner=1 if Q223==398008
replace winner=0 if Q223==398003 | Q223==398004 | Q223==398005 | Q223==398009 | Q223==398011 
/*Taiwan 2019*/
replace winner=1 if Q223==158002
replace winner=0 if Q223==158001 | Q223==158003 | Q223==158013 | Q223==158015 | Q223==158021 | Q223==158022 | Q223==158023 | Q223==158024 | Q223==158025
replace winner=2 if Q223==158020 /*non-partisan*/
/*South Korea 2017-2018*/
replace winner=1 if Q223==410002
replace winner=0 if Q223==410001 | Q223==410008 | Q223==410009 | Q223==410010
/*Japan 2019*/
replace winner=1 if Q223==392001 | Q223==392003
replace winner=0 if Q223==392004 | Q223==392014 | Q223==392025 | Q223==392026 | Q223==392027
/*Pakistan 2018*/
replace winner=1 if Q223==586007 | Q223==586003 | Q223==586033 | Q223==586017
replace winner=0 if Q223==586001 | Q223==586002 | Q223==586016 | Q223==586019 | Q223==586025 | Q223==586026 | Q223==586028 | Q223==586030 | Q223==586034
/*Bangladesh 2018*/
replace winner=1 if Q223==50001 | Q223==50003
replace winner=0 if Q223==50002 | Q223==50004 | Q223==50009
/*Myanmar 2020*/
replace winner=1 if Q223==104001 | Q223==104002
replace winner=0 if Q223==104003 | Q223==104004 | Q223==104005
/*Thailand 2017-2018*/
/*military rule so no winner*/
replace winner=0 if Q223==764001 | Q223==764005 | Q223==764006 | Q223==764008 | Q223==764010
/*Vietnam 2019-2020*/
replace winner=1 if cowcode==816 /*one-party system so everyone's a winner (baby!)*/
/*Malaysia 2018*/
replace winner=1 if (Q223==458006 & split==1) | (Q223==458009 & split==1)
replace winner=0 if (Q223==458005 & split==1) | (Q223==458004 & split==1) | (Q223==458007 & split==1) | (Q223==458008 & split==1)
replace winner=1 if (Q223==458005 & split==2)
replace winner=0 if (Q223==458006 & split==2) | (Q223==458009 & split==2) | (Q223==458004 & split==2) | (Q223==458007 & split==2) | (Q223==458008 & split==2)
/*Philippines 2019*/
replace winner=1 if Q223==608005 | Q223==608003 | Q223==608004 | Q223==608034  | Q223==608001 /*National Unity Party, Lakas-CMD were part of Duterte's Coalition for Change as well*/
replace winner=0 if Q223==608002 | Q223==608013 | Q223==608014 | Q223==608035
/*Indonesia 2018*/
replace winner=1 if Q223==360007 | Q223==360002 | Q223==360014 | Q223==360004 | Q223==360017 | Q223==360003 | Q223==360005
replace winner=0 if Q223==360001 | Q223==360011 | Q223==360012 | Q223==360013 | Q223==360015 | Q223==360018 | Q223==360019
/*Australia 2018*/
replace winner=1 if Q223==36002 | Q223==36003
replace winner=0 if Q223==36001 | Q223==36004 | Q223==36005 | Q223==36008 | Q223==36009 | Q223==36010 | Q223==36011
/*New Zealand 2019-2020*/
replace winner=1 if Q223==554005 | Q223==554007 | Q223==554004
replace winner=0 if Q223==554001 | Q223==554006 | Q223==554009 | Q223==554013 | Q223==554014
/*Serbia 2018*/
replace winner=1 if Q223==688002 | Q223==688003 | Q223==688009
replace winner=0 if Q223==688001 | Q223==688004 | Q223==688005 | Q223==688006 | Q223==688007 | Q223==688008


/*recoding EVS 5 countries*/
replace Q223=v174_cs if study==1
lab val Q223 V174_CS

/*United Kingdom 2018*/
replace winner=1 if Q223==82601
replace winner=0 if Q223==82602 | Q223==82603 | Q223==82604 | Q223==82605 | Q223==82606 | Q223==82607 | Q223==82608
replace winner=2 if Q223==82609
replace winner=3 if Q223==82666
/*Netherlands 2017-2018*/
replace winner=1 if Q223==52801 | Q223==52803 | Q223==52808 | Q223==52804
replace winner=0 if Q223==52802 | Q223==52805 | Q223==52806 | Q223==52807 | Q223==52809 | Q223==52810 | Q223==52811 | Q223==52812 | Q223==52813
replace winner=2 if Q223==52814
replace winner=3 if Q223==52866
/*France 2018*/
replace winner=1 if Q223==25008 | Q223==25009 
replace winner=0 if Q223==25001 | Q223==25002 | Q223==25003 | Q223==25004 | Q223==25005 | Q223==25006 | Q223==25007 | Q223==25010 | Q223==25011 | Q223==25012 | Q223==25013 | Q223==25014 | Q223==25015
replace winner=2 if Q223==25026
replace winner=3 if Q223==25066
/*Switzerland 2017-2018*/
replace winner=1 if Q223==75602 | Q223==75604 | Q223==75601 | Q223==75603
replace winner=0 if Q223==75605 | Q223==75606 | Q223==75607 | Q223==75608 | Q223==75609 | Q223==75610 | Q223==75611 | Q223==75612 | Q223==75613 | Q223==75614
replace winner=2 if Q223==75626
replace winner=3 if Q223==75666
/*Spain 2017-2018*/
replace winner=1 if Q223==72402
replace winner=0 if Q223==72401 | Q223==72403 | Q223==72404 | Q223==72405 | Q223==72406 | Q223==72407 | Q223==72408 | Q223==72409 | Q223==72409 | Q223==72410 | Q223==72411 | Q223==72412 | Q223==72413 | Q223==72415 | Q223==72418 | Q223==72420 | Q223==72421
replace winner=2 if Q223==72426
replace winner=3 if Q223==72466
/*Germany 2017-2018*/
replace winner=1 if Q223==27601 | Q223==27602
replace winner=0 if Q223==27603 | Q223==27604 | Q223==27605 | Q223==27606
replace winner=2 if Q223==27607
/*Poland 2017-2018*/
replace winner=1 if Q223==61601 | Q223==61608
replace winner=0 if Q223==61602 | Q223==61603 | Q223==61604 | Q223==61605 | Q223==61606 | Q223==61607
replace winner=2 if Q223==61609
replace winner=3 if Q223==61666
/*Austria 2018*/
replace winner=1 if Q223==4002 | Q223==4003
replace winner=0 if Q223==4001 | Q223==4004 | Q223==4005 | Q223==4006
replace winner=2 if Q223==4026
replace winner=3 if Q223==4066
/*Hungary 2018*/
replace winner=1 if Q223==34802 | Q223==34803
replace winner=0 if Q223==34801 | Q223==34804 | Q223==34805 | Q223==34806 | Q223==34807 | Q223==34808 | Q223==34809 | Q223==34810 | Q223==34811 | Q223==34812
replace winner=2 if Q223==34826
replace winner=3 if Q223==34866
/*Czechia 2017*/
replace winner=1 if (split==1 & Q223==20301)
replace winner=0 if (split==1 & Q223==20302) | (split==1 & Q223==20303) | (split==1 & Q223==20304) | (split==1 & Q223==20305) | (split==1 & Q223==20306) | (split==1 & Q223==20307) | (split==1 & Q223==20308) | (split==1 & Q223==20309) | (split==1 & Q223==20310) | (split==1 & Q223==20311) | (split==1 & Q223==20312) | (split==1 & Q223==20313) | (split==1 & Q223==20314) | (split==1 & Q223==20315)
replace winner=1 if (split==2 & Q223==20302) | (split==2 & Q223==20305) | (split==2 & Q223==20301)
replace winner=0 if (split==2 & Q223==20303) | (split==2 & Q223==20304) | (split==2 & Q223==20306) | (split==2 & Q223==20307) | (split==2 & Q223==20308) | (split==2 & Q223==20309) | (split==2 & Q223==20310) | (split==2 & Q223==20311) | (split==2 & Q223==20312) | (split==2 & Q223==20313) | (split==2 & Q223==20314) | (split==2 & Q223==20315)
replace winner=2 if Q223==20316
replace winner=2 if Q223==20366
/*Slovakia 2017*/
replace winner=1 if Q223==70310 | Q223==70308 | Q223==70304 | Q223==70306
replace winner=0 if Q223==70301 | Q223==70302 | Q223==70303 | Q223==70305 | Q223==70307 | Q223==70309 | Q223==70311 | Q223==70312
replace winner=3 if Q223==70366
/*Italy 2018-2019*/
replace winner=1 if Q223==38009 | Q223==38013
replace winner=0 if Q223==38001 | Q223==38002 | Q223==38003 | Q223==38004 | Q223==38005 | Q223==38006 | Q223==38007 | Q223==38008 | Q223==38010 | Q223==38011 | Q223==38012 | Q223==38014 | Q223==38015 | Q223==38016
replace winner=2 if Q223==38031
replace winner=3 if Q223==38066
/*Albania 2018*/
replace winner=1 if Q223==802 | Q223==803
replace winner=0 if Q223==801 | Q223==804 | Q223==806 | Q223==808 | Q223==809 | Q223==810 | Q223==811 | Q223==813 | Q223==814 | Q223==817 | Q223==819 | Q223==820 | Q223==821 | Q223==822
replace winner=2 if Q223==826
replace winner=3 if Q223==866
/*Montenegro 2019*/
replace winner=1 if Q223==49901 | Q223==49913 | Q223==49903 | Q223==49915 | Q223==49918
replace winner=0 if Q223==49902 | Q223==49904 | Q223==49905 | Q223==49906 | Q223==49907 | Q223==49908 | Q223==49909 | Q223==49910 | Q223==49911 | Q223==49912 | Q223==49916 | Q223==49917 | Q223==49919
replace winner=2 if Q223==49926
replace winner=3 if Q223==49966
/*Macedonia 2018-2019*/
replace winner=1 if Q223==80716  | Q223==80708 | Q223==80711 | Q223==80710 | Q223==80709 | Q223==80712 | Q223==80715
replace winner=0 if Q223==80701 | Q223==80702 | Q223==80703 | Q223==80704 | Q223==80705 | Q223==80707 | Q223==80710 | Q223==80713 | Q223==80714 | Q223==80717 | Q223==80718 | Q223==80719
replace winner=2 if Q223==80731
replace winner=3 if Q223==80766
/*Croatia 2017-2018*/
replace winner=1 if Q223==19102 | Q223==19103
replace winner=0 if Q223==19101 | Q223==19104 | Q223==19105 | Q223==19106 | Q223==19107 | Q223==19108 | Q223==19109 | Q223==19110 | Q223==19111 | Q223==19112 | Q223==19113 | Q223==19114 | Q223==19115
replace winner=2 if Q223==19126
/*Bosnia and Herzegovina 2019*/
replace winner=1 if Q223==7012 | Q223==7017 | Q223==7004 | Q223==7002 | Q223==7016
replace winner=0 if Q223==7001 | Q223==7003 | Q223==7005 | Q223==7006 | Q223==7008 | Q223==7009 | Q223==7010 | Q223==7011 | Q223==7013 | Q223==7014 | Q223==7015 | Q223==7019
replace winner=2 if Q223==7031
replace winner=3 if Q223==7066
/*Slovenia 2017*/
replace winner=1 if Q223==70501 | Q223==70504 | Q223==70503
replace winner=0 if Q223==70502 | Q223==70505 | Q223==70506 | Q223==70507 | Q223==70508 | Q223==70509 | Q223==70510
replace winner=2 if Q223==70511
/*Bulgaria 2017-2018*/
replace winner=1 if Q223==10001 | Q223==10004 | Q223==10007 | Q223==10015
replace winner=0 if Q223==10002 | Q223==10003 | Q223==10005 | Q223==10006 | Q223==10008 | Q223==10009 | Q223==10010 | Q223==10011 | Q223==10012 | Q223==10013 | Q223==10014
/*Romania 2018*/
replace winner=1 if Q223==64201 | Q223==64206
replace winner=0 if Q223==64202 | Q223==64203 | Q223==64204 | Q223==64205 | Q223==64207
replace winner=2 if Q223==64226
replace winner=3 if Q223==64266
/*Russia 2017*/
replace winner=1 if Q223==64304 | Q223==64301 | Q223==64313 /*Rodina and Patriots of Russia are party of All Russia Front*/
replace winner=0 if Q223==64302 | Q223==64303 | Q223==64305 | Q223==64306 | Q223==64307 | Q223==64308 | Q223==64309 | Q223==64310 | Q223==64311 | Q223==64312 | Q223==64314
replace winner=3 if Q223==64366
/*Estonia 2018*/
replace winner=1 if Q223==23301 | Q223==23303 | Q223==23304
replace winner=0 if Q223==23302 | Q223==23305 | Q223==23306 | Q223==23307
replace winner=2 if Q223==23331
replace winner=3 if Q223==23366
/*Lithuania 2017-2018*/
replace winner=1 if Q223==44007 | Q223==44006
replace winner=0 if Q223==44001 | Q223==44002 | Q223==44003 | Q223==44004 | Q223==44005 | Q223==44008 | Q223==44009 | Q223==44010 | Q223==44011
replace winner=2 if Q223==44012
replace winner=3 if Q223==44066
/*Belarus 2018*/
replace winner=1 if Q223==11216
replace winner=0 if Q223==11201 | Q223==11202 | Q223==11203 | Q223==11204 | Q223==11205 | Q223==11206 | Q223==11207 | Q223==11208 | Q223==11209 | Q223==11210 | Q223==11211 | Q223==11212 | Q223==11213 | Q223==11214 | Q223==11215
replace winner=2 if Q223==11226
/*Armenia 2018*/
replace winner=1 if Q223==5110 | Q223==5101
replace winner=0 if Q223==5102 | Q223==5103 | Q223==5104 | Q223==5105 | Q223==5106 | Q223==5107 | Q223==5108 | Q223==5109 | Q223==5111 | Q223==5112
/*Georgia 2018*/
replace winner=1 if Q223==26810
replace winner=0 if Q223==26801 | Q223==26802 | Q223==26803 | Q223==26804 | Q223==26805 | Q223==26806 | Q223==26807 | Q223==26808 | Q223==26809 | Q223==26811 | Q223==26812 | Q223==26813 | Q223==26814 | Q223==26815
replace winner=2 if Q223==26826
replace winner=3 if Q223==26866
/*Azerbaijan 2018*/
replace winner=1 if Q223==3101
replace winner=0 if Q223==3102 | Q223==3103 | Q223==3104 | Q223==3106 | Q223==3107 | Q223==3108 | Q223==3109 | Q223==3110 | Q223==3111 | Q223==3112 | Q223==3115 | Q223==3119
replace winner=2 if Q223==3131
replace winner=3 if Q223==3166
/*Finland 2017-2018*/
replace winner=1 if Q223==24601 | Q223==24602 | Q223==24608
replace winner=0 if Q223==24603 | Q223==24604 | Q223==24605 | Q223==24606 | Q223==24607
replace winner=2 if Q223==24609
replace winner=3 if Q223==24666
/*Sweden 2017-2018*/
replace winner=1 if Q223==75201 | Q223==75207
replace winner=0 if Q223==75202 | Q223==75203 | Q223==75204 | Q223==75205 | Q223==75206 | Q223==75208
replace winner=2 if Q223==75231
/*Norway 2018*/
replace winner=1 if Q223==57804 | Q223==57813 | Q223==57803
replace winner=0 if Q223==57801 | Q223==57802 | Q223==57805 | Q223==57806 | Q223==57807 | Q223==57808 | Q223==57809 | Q223==57810 | Q223==57811 | Q223==57812
replace winner=2 if Q223==57814
/*Denmark 2017-2018*/
replace winner=1 if Q223==20807 | Q223==20803 | Q223==20808
replace winner=0 if Q223==20801 | Q223==20802 | Q223==20804 | Q223==20805 | Q223==20806 | Q223==20809 | Q223==20810
replace winner=3 if Q223==20811
/*Iceland 2017-2018*/
replace winner=1 if (split==1 & Q223==35204) | (split==1 & Q223==35201) | (split==1 & Q223==35202)
replace winner=0 if (split==1 & Q223==35203) | (split==1 & Q223==35205) | (split==1 & Q223==35206) | (split==1 & Q223==35207) | (split==1 & Q223==35208) | (split==1 & Q223==35209) | (split==1 & Q223==35210) | (split==1 & Q223==35211) | (split==1 & Q223==35212) | (split==1 & Q223==35216)
replace winner=1 if (split==2 & Q223==35201) | (split==2 & Q223==35205) | (split==2 & Q223==35207)
replace winner=0 if (split==2 & Q223==35202) | (split==2 & Q223==35203) | (split==2 & Q223==35204) | (split==2 & Q223==35206) | (split==2 & Q223==35208) | (split==2 & Q223==35209) | (split==2 & Q223==35210) | (split==2 & Q223==35211) | (split==2 & Q223==35212) | (split==2 & Q223==35216)
replace winner=2 if Q223==35226
replace winner=3 if Q223==35266
/*Serbia 2018*/
replace winner=1 if Q223==68801 | Q223==68810 | Q223==68807 | Q223==68804 | Q223==68813
replace winner=0 if Q223==68802 | Q223==68803 | Q223==68805 | Q223==68806 | Q223==68809 | Q223==68811 | Q223==68812 | Q223==68814 | Q223==68815 | Q223==68816 | Q223==68818 | Q223==68819 | Q223==68820
replace winner=2 if Q223==68831


/*recoding WVS 6 countries*/
tab V228
replace winner=2 if V228==5
replace winner=3 if V228==4 | V228==3 | V228==2 | V228==7
replace winner=4 if V228==1

/*Netherlands 2012*/
replace winner=1 if V228==528003 | V228==528001
replace winner=0 if V228==528002 | V228==528004 | V228==528005 | V228==528006 | V228==528009 | V228==528013 | V228==528014 | V228==528015 | V228==528018
/*Romania 2012*/
replace winner=1 if (split==1 & V228==642036) | (split==1 & V228==642034) | (split==1 & V228==642060)
replace winner=0 if (split==1 & V228==642004) | (split==1 & V228==642006) | (split==1 & V228==642007) | (split==1 & V228==642035) | (split==1 & V228==642039) | (split==1 & V228==642052) | (split==1 & V228==642055) | (split==1 & V228==642056) | (split==1 & V228==642062) | (split==1 & V228==642063)
/*Germany 2013*/
replace winner=1 if (split==1 & V228==276001) | (split==1 & V228==276002)
replace winner=0 if (split==1 & V228==276003) | (split==1 & V228==276004) | (split==1 & V228==276005) | (split==1 & V228==276016) | (split==1 & V228==276017) | (split==1 & V228==276018) | (split==1 & V228==276026) | (split==1 & V228==276031) | (split==1 & V228==276032) | (split==1 & V228==276033)
/*Georgia 2014*/
replace winner=1 if V228==268123 | V228==268109 | V228==268004
replace winner=0 if V228==268101 | V228==268103 | V228==268104 | V228==268105 | V228==268108 | V228==268111 | V228==268116 | V228==268121 | V228==268125 | V228==268126 | V228==268127
replace winner=3 if V228==268128
/*Jordan 2014*/
/*no winners*/
replace winner=0 if V228==400002 | V228==400003 | V228==400005 | V228==400006 | V228==400014 | V228==400020 | V228==400030 | V228==400031 | V228==400032 | V228==400033 | V228==400034 | V228==400035 | V228==400036 | V228==400037 | V228==400038 | V228==400039
/*Haiti 2015-2016*/
/*no winners as election did not result in government formation*/
replace winner=0 if V228==332001 | V228==332002 | V228==332003 | V228==332004 | V228==332005 | V228==332006 | V228==332007 | V228==332008 | V228==332009

mvdecode winner, mv(2/5)
save survey.dta, replace


**************************************************
         ***preparing the macro data***
**************************************************

//** World Development Indicators **//
clear
import delimited using wdi.csv
rename countryname country_name
rename time eleyear
drop timecode countrycode
rename gdpgrowth gdpgrowth
rename individual internet

replace gdpgrowth="." if gdpgrowth==".."
replace internet="." if internet==".."
destring gdpgrowth internet, replace
sum gdpgrowth internet

gen cowcode=.
replace cowcode=2 if country_name=="United States"
replace cowcode=20 if country_name=="Canada"
replace cowcode=31 if country_name=="Bahamas, The"
replace cowcode=40 if country_name=="Cuba"
replace cowcode=41 if country_name=="Haiti"
replace cowcode=42 if country_name=="Dominican Republic"
replace cowcode=51 if country_name=="Jamaica"
replace cowcode=52 if country_name=="Trinidad and Tobago"
replace cowcode=53 if country_name=="Barbados"
replace cowcode=54 if country_name=="Dominica"
replace cowcode=55 if country_name=="Grenada"
replace cowcode=56 if country_name=="St. Lucia"
replace cowcode=57 if country_name=="St. Vincent and the Grenadines"
replace cowcode=58 if country_name=="Antigua and Barbuda"
replace cowcode=60 if country_name=="St. Kitts and Nevis"
replace cowcode=70 if country_name=="Mexico"
replace cowcode=80 if country_name=="Belize"
replace cowcode=90 if country_name=="Guatemala"
replace cowcode=91 if country_name=="Honduras"
replace cowcode=92 if country_name=="El Salvador"
replace cowcode=93 if country_name=="Nicaragua"
replace cowcode=94 if country_name=="Costa Rica"
replace cowcode=95 if country_name=="Panama"
replace cowcode=100 if country_name=="Colombia"
replace cowcode=101 if country_name=="Venezuela, RB"
replace cowcode=110 if country_name=="Guyana"
replace cowcode=115 if country_name=="Suriname"
replace cowcode=130 if country_name=="Ecuador"
replace cowcode=135 if country_name=="Peru"
replace cowcode=140 if country_name=="Brazil"
replace cowcode=145 if country_name=="Bolivia"
replace cowcode=150 if country_name=="Paraguay"
replace cowcode=155 if country_name=="Chile"
replace cowcode=160 if country_name=="Argentina"
replace cowcode=165 if country_name=="Uruguay"
replace cowcode=200 if country_name=="United Kingdom"
replace cowcode=205 if country_name=="Ireland"
replace cowcode=210 if country_name=="Netherlands"
replace cowcode=211 if country_name=="Belgium"
replace cowcode=212 if country_name=="Luxembourg"
replace cowcode=220 if country_name=="France"
replace cowcode=221 if country_name=="Monaco"
replace cowcode=223 if country_name=="Liechtenstein"
replace cowcode=225 if country_name=="Switzerland"
replace cowcode=230 if country_name=="Spain"
replace cowcode=232 if country_name=="Andorra"
replace cowcode=235 if country_name=="Portugal"
replace cowcode=255 if country_name=="Germany"
replace cowcode=290 if country_name=="Poland"
replace cowcode=305 if country_name=="Austria"
replace cowcode=310 if country_name=="Hungary"
replace cowcode=315 if country_name=="Czechoslovakia"
replace cowcode=316 if country_name=="Czechia"
replace cowcode=317 if country_name=="Slovak Republic"
replace cowcode=325 if country_name=="Italy"
replace cowcode=331 if country_name=="San Marino"
replace cowcode=338 if country_name=="Malta"
replace cowcode=339 if country_name=="Albania"
replace cowcode=341 if country_name=="Montenegro"
replace cowcode=343 if country_name=="North Macedonia"
replace cowcode=344 if country_name=="Croatia"
replace cowcode=345 if country_name=="Yugoslavia"
replace cowcode=346 if country_name=="Bosnia and Herzegovina"
replace cowcode=347 if country_name=="Kosovo"
replace cowcode=349 if country_name=="Slovenia"
replace cowcode=350 if country_name=="Greece"
replace cowcode=352 if country_name=="Cyprus"
replace cowcode=355 if country_name=="Bulgaria"
replace cowcode=359 if country_name=="Moldova"
replace cowcode=360 if country_name=="Romania"
replace cowcode=365 if country_name=="Russian Federation"
replace cowcode=366 if country_name=="Estonia"
replace cowcode=367 if country_name=="Latvia"
replace cowcode=368 if country_name=="Lithuania"
replace cowcode=369 if country_name=="Ukraine"
replace cowcode=370 if country_name=="Belarus"
replace cowcode=371 if country_name=="Armenia"
replace cowcode=372 if country_name=="Georgia"
replace cowcode=373 if country_name=="Azerbaijan"
replace cowcode=375 if country_name=="Finland"
replace cowcode=380 if country_name=="Sweden"
replace cowcode=385 if country_name=="Norway"
replace cowcode=390 if country_name=="Denmark"
replace cowcode=395 if country_name=="Iceland"
replace cowcode=402 if country_name=="Cabo Verde"
replace cowcode=403 if country_name=="Sao Tome and Principe"
replace cowcode=404 if country_name=="Guinea-Bissau"
replace cowcode=411 if country_name=="Equatorial Guinea"
replace cowcode=420 if country_name=="Gambia, The"
replace cowcode=432 if country_name=="Mali"
replace cowcode=433 if country_name=="Senegal"
replace cowcode=434 if country_name=="Benin"
replace cowcode=435 if country_name=="Mauritania"
replace cowcode=436 if country_name=="Niger"
replace cowcode=437 if country_name=="Cote d'Ivoire"
replace cowcode=438 if country_name=="Guinea"
replace cowcode=439 if country_name=="Burkina Faso"
replace cowcode=450 if country_name=="Liberia"
replace cowcode=451 if country_name=="Sierra Leone"
replace cowcode=452 if country_name=="Ghana"
replace cowcode=461 if country_name=="Togo"
replace cowcode=471 if country_name=="Cameroon"
replace cowcode=475 if country_name=="Nigeria"
replace cowcode=481 if country_name=="Gabon"
replace cowcode=482 if country_name=="Central African Republic"
replace cowcode=483 if country_name=="Chad"
replace cowcode=484 if country_name=="Congo, Rep."
replace cowcode=490 if country_name=="Congo, Dem. Rep."
replace cowcode=500 if country_name=="Uganda"
replace cowcode=501 if country_name=="Kenya"
replace cowcode=510 if country_name=="Tanzania"
replace cowcode=516 if country_name=="Burundi"
replace cowcode=517 if country_name=="Rwanda"
replace cowcode=520 if country_name=="Somalia"
replace cowcode=522 if country_name=="Djibouti"
replace cowcode=530 if country_name=="Ethiopia"
replace cowcode=531 if country_name=="Eritrea"
replace cowcode=540 if country_name=="Angola"
replace cowcode=541 if country_name=="Mozambique"
replace cowcode=551 if country_name=="Zambia"
replace cowcode=552 if country_name=="Zimbabwe"
replace cowcode=553 if country_name=="Malawi"
replace cowcode=560 if country_name=="South Africa"
replace cowcode=565 if country_name=="Namibia"
replace cowcode=570 if country_name=="Lesotho"
replace cowcode=571 if country_name=="Botswana"
replace cowcode=572 if country_name=="Eswatini"
replace cowcode=580 if country_name=="Madagascar"
replace cowcode=581 if country_name=="Comoros"
replace cowcode=590 if country_name=="Mauritius"
replace cowcode=591 if country_name=="Seychelles"
replace cowcode=600 if country_name=="Morocco"
replace cowcode=615 if country_name=="Algeria"
replace cowcode=616 if country_name=="Tunisia"
replace cowcode=620 if country_name=="Libya"
replace cowcode=625 if country_name=="Sudan"
replace cowcode=626 if country_name=="South Sudan"
replace cowcode=630 if country_name=="Iran, Islamic Rep."
replace cowcode=640 if country_name=="Turkiye"
replace cowcode=645 if country_name=="Iraq"
replace cowcode=651 if country_name=="Egypt, Arab Rep."
replace cowcode=652 if country_name=="Syrian Arab Republic"
replace cowcode=660 if country_name=="Lebanon"
replace cowcode=663 if country_name=="Jordan"
replace cowcode=666 if country_name=="Israel"
replace cowcode=670 if country_name=="Saudi Arabia"
replace cowcode=679 if country_name=="Yemen, Rep."
replace cowcode=690 if country_name=="Kuwait"
replace cowcode=692 if country_name=="Bahrain"
replace cowcode=694 if country_name=="Qatar"
replace cowcode=696 if country_name=="United Arab Emirates"
replace cowcode=698 if country_name=="Oman"
replace cowcode=700 if country_name=="Afghanistan"
replace cowcode=701 if country_name=="Turkmenistan"
replace cowcode=702 if country_name=="Tajikistan"
replace cowcode=703 if country_name=="Kyrgyz Republic"
replace cowcode=704 if country_name=="Uzbekistan"
replace cowcode=705 if country_name=="Kazakhstan"
replace cowcode=710 if country_name=="China"
replace cowcode=712 if country_name=="Mongolia"
replace cowcode=713 if country_name=="Taiwan"
replace cowcode=714 if country_name=="Hong Kong SAR, China"
replace cowcode=715 if country_name=="Macao SAR, China"
replace cowcode=731 if country_name=="Korea, Dem. People's Rep."
replace cowcode=732 if country_name=="Korea, Rep."
replace cowcode=740 if country_name=="Japan"
replace cowcode=750 if country_name=="India"
replace cowcode=760 if country_name=="Bhutan"
replace cowcode=770 if country_name=="Pakistan"
replace cowcode=771 if country_name=="Bangladesh"
replace cowcode=775 if country_name=="Myanmar"
replace cowcode=780 if country_name=="Sri Lanka"
replace cowcode=781 if country_name=="Maldives"
replace cowcode=790 if country_name=="Nepal"
replace cowcode=800 if country_name=="Thailand"
replace cowcode=811 if country_name=="Cambodia"
replace cowcode=812 if country_name=="Lao PDR"
replace cowcode=816 if country_name=="Vietnam"
replace cowcode=820 if country_name=="Malaysia"
replace cowcode=830 if country_name=="Singapore"
replace cowcode=835 if country_name=="Brunei Darussalam"
replace cowcode=840 if country_name=="Philippines"
replace cowcode=850 if country_name=="Indonesia"
replace cowcode=860 if country_name=="Timor-Leste"
replace cowcode=900 if country_name=="Australia"
replace cowcode=910 if country_name=="Papua New Guinea"
replace cowcode=920 if country_name=="New Zealand"
replace cowcode=935 if country_name=="Vanuatu"
replace cowcode=940 if country_name=="Solomon Islands"
replace cowcode=946 if country_name=="Kiribati"
replace cowcode=947 if country_name=="Tuvalu"
replace cowcode=950 if country_name=="Fiji"
replace cowcode=955 if country_name=="Tonga"
replace cowcode=970 if country_name=="Nauru"
replace cowcode=983 if country_name=="Marshall Islands"
replace cowcode=986 if country_name=="Palau"
replace cowcode=987 if country_name=="Micronesia, Fed. Sts."
replace cowcode=990 if country_name=="Samoa"
replace cowcode=993 if country_name=="Serbia"
replace cowcode=994 if country_name=="West Bank and Gaza"

drop if cowcode==.
sort cowcode eleyear
replace internet = internet[_n-1] if internet==. & cowcode==cowcode[_n-1] /*replace mmissing internet penetration with data from previous year - WDI has very little data from 2018+*/
save wdi.dta, replace


//** Varieties-of-Democracy data **//
use V-Dem-CY-Full+Others-v11.1.dta
drop if year <2005
rename COWcode cowcode
rename year eleyear
drop if cowcode==. /*V-Dem doesnt assign cowcodes for Hong Kong, Palestine, and Somaliland, but that's not important as we dont have PEI/WVS data for these countries anyway, so we can just drop them*/
replace cowcode=993 if cowcode==345 /*replace cowcode for Serbia*/

drop v2elaccept v2smgovdom v2smfordom v2smpardom
gen v2elaccept = (4-v2elaccept_osp)/4
gen v2smgovdom = (4-v2smgovdom_osp)/4
gen v2smfordom = (4-v2smfordom_osp)/4
gen v2smpardom = (4-v2smpardom_osp)/4
replace v2eltrnout = v2eltrnout/100

sum cowcode eleyear v2x_regime v2smgovdom v2smpardom v2smfordom v2xme_altinf v2eltrnout v2elaccept v2xel_frefair v2x_libdem
keep cowcode eleyear v2x_regime v2smgovdom v2smpardom v2smfordom v2xme_altinf v2eltrnout v2elaccept v2xel_frefair v2x_libdem

gen democracy = .
replace democracy = 1 if v2x_regime==2 | v2x_regime==3
replace democracy = 0 if v2x_regime==0 | v2x_regime==1
tab democracy v2x_regime

save vdem11.dta, replace

//** Perceptions of Electoral Integrity election-level data **//	
use PEI election-level data (PEI_7.0) v2 09-05-2019.dta
rename ccodecow cowcode
rename year eleyear
gen pei1 = (faircount-1)/4
gen pei2 = (oppprevent2-1)/4
gen pei6 = (fairofficials-1)/4
gen pei7 = (rich2-1)/4
gen pei8 = (violence2-1)/4
gen dommonrestr = (domesticmonitors2-1)/4
gen intmonrestr = (intlmonitors2-1)/4
gen newsinaccurate = 1-((campaignnews-1)/4)
gen newsfake = (fakenews-1)/4

keep cowcode eleyear pei* dommonrestr intmonrestr election newsinaccurate newsfake
egen eleid = group(election)
save pei.dta, replace


//** Perceptions of Electoral Integrity expert-level data (robustness checks only) **//	
use PEI expert-level data (PEI_7.0) v2 09-05-2019.dta

** keep only data for elections in merged dataset
egen eleid = group(election)
keep if election=="USA_08112016_P1" | election=="MEX_07062015_L1" | election=="NIC_06112016_P1" | election=="COL_17062018_P2" | election=="ECU_02042017_P2" | election=="PER_05062016_P2" | election=="BRA_26102014_P2" | election=="BOL_12102014_P1" | election=="CHL_17122017_P2" | election=="ARG_22112015_L1" | election=="GBR_08062017_L1" | election=="NLD_15032017_L1" | election=="FRA_07052017_P2" | election=="CHE_18102015_L1" | election=="ESP_26062016_L1" | election=="DEU_24092017_L1" | election=="POL_24052015_P2" | election=="AUT_15102017_L1" | election=="SVK_05032016_L1" | election=="ITA_04032018_L1" | election=="ALB_25062017_L1" | election=="MNE_15042018_P1" | election=="MKD_11122016_L1" | election=="HRV_11092016_L1" | election=="BIH_07102018_P1" | election=="GRC_20092015_L1" | election=="CYP_04022018_P2" | election=="BGR_26032017_L1" | election=="ROU_11122016_L1" | election=="RUS_18092016_L1" | election=="EST_01032015_L1" | election=="LTU_09102016_L1" | election=="BLR_11092016_L1" | election=="ARM_02042017_L1" | election=="GEO_08102016_L1" | election=="AZE_11042018_P1" | election=="SWE_14092014_L1" | election=="NOR_09112017_L1" | election=="DNK_18062015_L1" | election=="NGA_28032015_L1" | election=="ETH_24052015_L1" | election=="ZWE_30072018_P1" | election=="TUN_21122014_P2" | election=="IRN_19052017_P1" | election=="TUR_01112015_L1" | election=="IRQ_12052018_L1" | election=="EGY_28032018_P1" | election=="LBN_06052018_L1" | election=="TJK_01032015_L1" | election=="KGZ_15102017_P1" | election=="KAZ_20032016_L1" | election=="TWN_16012016_P1" | election=="KOR_09052017_P1" | election=="JPN_22102017_L1" | election=="PAK_25072018_L1" | election=="BGD_05012014_L1" | election=="MMR_08112015_L1" | election=="THA_02022014_L1" | election=="VNM_22052016_L1" | election=="PHL_09052016_P1" | election=="IDN_09072014_P1" | election=="AUS_02072016_L1" | election=="NZL_23092017_L1" | election=="SRB_02042017_P1" | election=="NLD_12092012_L1" | election=="GEO_27102013_P1" | election=="JOR_23012013_L1" | election=="HTI_25102015_L2" | election=="HUN_08042018_L1" | election=="HUN_06042014_L1" | election=="CZE_21102017_L1" | election=="CZE_25102013_L1" | election=="SVN_12112017_P2" | election=="SVN_13072014_L1" | election=="FIN_28012018_P1" | election=="FIN_19042015_L1" | election=="ISL_28102017_L1" | election=="ISL_25062016_P1" | election=="MYS_09052018_L1" | election=="MYS_05052013_L1" | election=="ROU_09122012_L1" | election=="DEU_22092013_L1"

** recode variables of interest
gen pei1 = (faircount-1)/4
gen pei2 = (oppprevent2-1)/4
gen pei6 = (fairofficials-1)/4
gen pei7 = (rich2-1)/4
gen pei8 = (violence2-1)/4

save pei_exp.dta, replace


//** NELDA data (robustness checks only) **//
use id & q-wide_share.dta
drop if year <2010

** recode variable of interest (NELDA11)
encode nelda11, gen(nelda11new) 
replace nelda11new=. if nelda11new==2
drop nelda11
gen nelda11 = 1-(nelda11new-1)/2

** generate election variable for merge
rename ccode cowcode
replace cowcode=255 if cowcode==260 /*Germany*/
replace cowcode=993 if cowcode==340 /*Serbia*/
rename year eleyear
gen election = .
tostring election, replace
replace election="USA_08112016_P1" if cowcode==2 & eleyear==2016 & strpos(electionid, "P1")
replace election="MEX_07062015_L1" if cowcode==70 & eleyear==2015 & strpos(electionid, "L1")
replace election="NIC_06112016_P1" if cowcode==93 & eleyear==2016 & strpos(electionid, "P1")
replace election="COL_17062018_P2" if cowcode==100 & eleyear==2018 & strpos(electionid, "P2")
replace election="ECU_02042017_P2" if cowcode==130 & eleyear==2017 & strpos(electionid, "P2")
replace election="PER_05062016_P2" if cowcode==135 & eleyear==2016 & strpos(electionid, "P2")
replace election="BRA_26102014_P2" if cowcode==140 & eleyear==2014 & strpos(electionid, "P2")
replace election="BOL_12102014_P1" if cowcode==145 & eleyear==2014 & strpos(electionid, "P1")
replace election="CHL_17122017_P2" if cowcode==155 & eleyear==2017 & strpos(electionid, "P2")
replace election="ARG_22112015_L1" if cowcode==160 & eleyear==2015 & strpos(electionid, "P2")
replace election="GBR_08062017_L1" if cowcode==200 & eleyear==2017 & strpos(electionid, "L1")
replace election="NLD_15032017_L1" if cowcode==210 & eleyear==2017 & strpos(electionid, "L1")
replace election="FRA_07052017_P2" if cowcode==220 & eleyear==2017 & strpos(electionid, "P2")
replace election="CHE_18102015_L1" if cowcode==225 & eleyear==2015 & strpos(electionid, "L1")
replace election="ESP_26062016_L1" if cowcode==230 & eleyear==2016 & strpos(electionid, "L1")
replace election="DEU_24092017_L1" if cowcode==255 & eleyear==2017 & strpos(electionid, "L1")
replace election="POL_24052015_P2" if cowcode==290 & eleyear==2015 & strpos(electionid, "P2")
replace election="AUT_15102017_L1" if cowcode==305 & eleyear==2017 & strpos(electionid, "L1")
replace election="SVK_05032016_L1" if cowcode==317 & eleyear==2016 & strpos(electionid, "L1")
replace election="ITA_04032018_L1" if cowcode==325 & eleyear==2018 & strpos(electionid, "L1")
replace election="ALB_25062017_L1" if cowcode==339 & eleyear==2017 & strpos(electionid, "L1")
replace election="MNE_15042018_P1" if cowcode==341 & eleyear==2018 & strpos(electionid, "P1")
replace election="MKD_11122016_L1" if cowcode==343 & eleyear==2016 & strpos(electionid, "L1")
replace election="HRV_11092016_L1" if cowcode==344 & eleyear==2016 & strpos(electionid, "L1")
replace election="BIH_07102018_P1" if cowcode==346 & eleyear==2018 & strpos(electionid, "P1")
replace election="GRC_20092015_L1" if cowcode==350 & eleyear==2015 & strpos(electionid, "L1") & strpos(electionid, "0920") /*there were two L1 elections in Greece in 2015*/
replace election="CYP_04022018_P2" if cowcode==352 & eleyear==2018 & strpos(electionid, "P2")
replace election="BGR_26032017_L1" if cowcode==355 & eleyear==2017 & strpos(electionid, "L1")
replace election="ROU_11122016_L1" if cowcode==360 & eleyear==2016 & strpos(electionid, "L1")
replace election="RUS_18092016_L1" if cowcode==365 & eleyear==2016 & strpos(electionid, "L1")
replace election="EST_01032015_L1" if cowcode==366 & eleyear==2015 & strpos(electionid, "L1")
replace election="LTU_09102016_L1" if cowcode==368 & eleyear==2016 & strpos(electionid, "L1")
replace election="BLR_11092016_L1" if cowcode==370 & eleyear==2016 & strpos(electionid, "L1")
replace election="ARM_02042017_L1" if cowcode==371 & eleyear==2017 & strpos(electionid, "L1")
replace election="GEO_08102016_L1" if cowcode==372 & eleyear==2016 & strpos(electionid, "L1")
replace election="AZE_11042018_P1" if cowcode==373 & eleyear==2018 & strpos(electionid, "P1")
replace election="SWE_14092014_L1" if cowcode==380 & eleyear==2014 & strpos(electionid, "L1")
replace election="NOR_09112017_L1" if cowcode==385 & eleyear==2017 & strpos(electionid, "L1")
replace election="DNK_18062015_L1" if cowcode==390 & eleyear==2015 & strpos(electionid, "L1")
replace election="NGA_28032015_L1" if cowcode==475 & eleyear==2015 & strpos(electionid, "L1")
replace election="ETH_24052015_L1" if cowcode==530 & eleyear==2015 & strpos(electionid, "L1")
replace election="ZWE_30072018_P1" if cowcode==552 & eleyear==2018 & strpos(electionid, "P1")
replace election="TUN_21122014_P2" if cowcode==616 & eleyear==2014 & strpos(electionid, "P2")
replace election="IRN_19052017_P1" if cowcode==630 & eleyear==2017 & strpos(electionid, "P1")
replace election="TUR_01112015_L1" if cowcode==640 & eleyear==2015 & strpos(electionid, "L1") & strpos(electionid, "1101") /*there were two L1 elections in Turkey in 2015*/
replace election="IRQ_12052018_L1" if cowcode==645 & eleyear==2018 & strpos(electionid, "L1")
replace election="EGY_28032018_P1" if cowcode==651 & eleyear==2018 & strpos(electionid, "P1")
replace election="LBN_06052018_L1" if cowcode==660 & eleyear==2018 & strpos(electionid, "L1")
replace election="TJK_01032015_L1" if cowcode==702 & eleyear==2015 & strpos(electionid, "L1")
replace election="KGZ_15102017_P1" if cowcode==703 & eleyear==2017 & strpos(electionid, "P1")
replace election="KAZ_20032016_L1" if cowcode==705 & eleyear==2016 & strpos(electionid, "L1")
replace election="TWN_16012016_P1" if cowcode==713 & eleyear==2016 & strpos(electionid, "P1")
replace election="KOR_09052017_P1" if cowcode==732 & eleyear==2017 & strpos(electionid, "P1")
replace election="JPN_22102017_L1" if cowcode==740 & eleyear==2017 & strpos(electionid, "L1")
replace election="PAK_25072018_L1" if cowcode==770 & eleyear==2018 & strpos(electionid, "L1")
replace election="BGD_05012014_L1" if cowcode==771 & eleyear==2015 & strpos(electionid, "L1") /*for some reason, the 2014 election is coded as year==2015*/
replace election="MMR_08112015_L1" if cowcode==775 & eleyear==2015 & strpos(electionid, "L1")
replace election="THA_02022014_L1" if cowcode==800 & eleyear==2014 & strpos(electionid, "L1")
replace election="VNM_22052016_L1" if cowcode==816 & eleyear==2016 & strpos(electionid, "L1")
replace election="PHL_09052016_P1" if cowcode==840 & eleyear==2016 & strpos(electionid, "P1")
replace election="IDN_09072014_P1" if cowcode==850 & eleyear==2014 & strpos(electionid, "P1")
replace election="AUS_02072016_L1" if cowcode==900 & eleyear==2016 & strpos(electionid, "L1")
replace election="NZL_23092017_L1" if cowcode==920 & eleyear==2017 & strpos(electionid, "L1")
replace election="SRB_02042017_P1" if cowcode==993 & eleyear==2017 & strpos(electionid, "P1")

replace election="NLD_12092012_L1" if cowcode==210 & eleyear==2012 & strpos(electionid, "L1")
replace election="GEO_27102013_P1" if cowcode==372 & eleyear==2013 & strpos(electionid, "P1")
replace election="JOR_23012013_L1" if cowcode==663 & eleyear==2013 & strpos(electionid, "L1")
replace election="HTI_25102015_L2" if cowcode==41 & eleyear==2015 & strpos(electionid, "L2")

replace election="HUN_08042018_L1" if cowcode==310 & eleyear==2018 & strpos(electionid, "L1")
replace election="HUN_06042014_L1" if cowcode==310 & eleyear==2014 & strpos(electionid, "L1")
replace election="CZE_21102017_L1" if cowcode==316 & eleyear==2017 & strpos(electionid, "L1")
replace election="CZE_25102013_L1" if cowcode==316 & eleyear==2013 & strpos(electionid, "L1")
replace election="SVN_12112017_P2" if cowcode==349 & eleyear==2017 & strpos(electionid, "P2")
replace election="SVN_13072014_L1" if cowcode==349 & eleyear==2014 & strpos(electionid, "L1")
replace election="FIN_28012018_P1" if cowcode==375 & eleyear==2018 & strpos(electionid, "P1")
replace election="FIN_19042015_L1" if cowcode==375 & eleyear==2015 & strpos(electionid, "L1")
replace election="ISL_28102017_L1" if cowcode==395 & eleyear==2017 & strpos(electionid, "L1")
replace election="ISL_25062016_P1" if cowcode==395 & eleyear==2016 & strpos(electionid, "P1")
replace election="MYS_09052018_L1" if cowcode==820 & eleyear==2018 & strpos(electionid, "L1")
replace election="MYS_05052013_L1" if cowcode==820 & eleyear==2013 & strpos(electionid, "L1")
replace election="ROU_09122012_L1" if cowcode==360 & eleyear==2012 & strpos(electionid, "L1")
replace election="DEU_22092013_L1" if cowcode==255 & eleyear==2013 & strpos(electionid, "L1")

drop if election=="."
keep electionid election eleyear cowcode country types nelda11
save nelda.dta, replace


**************************************************
     ***merging survey with macro data***
**************************************************
use survey.dta

** merging with WDI data
merge m:1 cowcode eleyear using wdi.dta, keepusing(gdpgrowth internet)
tab cowcode if _merge==1 /*that's Taiwan as WDI does not record data for Taiwan plus countries for which we have no PEI data or respondents that were surveyed on election day (they need to be excluded b/c we don't know which election they'd refer to) - will be dropped after merge with V-Dem data*/
replace gdpgrowth=2.17 if cowcode==713 & eleyear==2016 /*adding Taiwan data by hand*/
replace internet=88 if cowcode==713 & eleyear==2016
tab cowcode if _merge==2 /*that's country-years for which we have no survey data*/
drop if _merge==2 
drop _merge

** merging with V-Dem data
merge m:1 cowcode eleyear using vdem11.dta
tab cowcode if _merge==1 /*that's countries for which we have no PEI data or respondents that were surveyed on election day (they need to be excluded b/c we don't know which election they'd refer to)*/
drop if _merge==1
tab cowcode if _merge==2 /*that's country-years for which we have V-Dem data but no survey data*/
drop if _merge==2
drop _merge

** merging with PEI data
merge m:1 election using pei.dta
tab cowcode if _merge==2 /*that's country-years for which we have PEI data but no survey data*/
drop if _merge==2
drop _merge

** merging with NELDA data
merge m:1 election using nelda.dta, keepusing(election cowcode nelda11)
drop _merge

** generate respondent id variable for importing data from mplus
gen id = _n
sum id
replace id=122475 if id==999 /*replace to avoid problem with missing values in Mplus*/

save mergeddata.dta, replace

** PEI expert-level data is not merged at this point since we first need to run CFA to create factor scores